<template>
  <div class="dialog-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['dispatch:routeStationAssess:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['dispatch:routeStationAssess:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['dispatch:routeStationAssess:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeStationAssessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点名称" align="center" prop="stationName" />
      <el-table-column label="考核时间" align="center" prop="assessTime" />
      <el-table-column label="允许提前时间(分)" align="center" prop="allowAheadTime" />
      <el-table-column label="允许延后时间(分)" align="center" prop="allowDelayTime" />
      <el-table-column label="考核开始时间" align="center" prop="assessStartTime" />
      <el-table-column label="考核结束时间" align="center" prop="assessEndTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_data_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改线路站点考核对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="routeStationAssessRef" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="站点考核时间(分)" prop="assessTime">
          <el-input v-model="form.assessTime" placeholder="请输入每日标准工作时长">
            <template #append>
              在考核时间提前此值后也算为准点
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="允许提前时间(分)" prop="allowAheadTime">
          <el-input v-model="form.allowAheadTime" placeholder="请输入每日标准工作时长">
            <template #append>
              据起始站需要考核的分钟数
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="允许延后时间(分)" prop="allowDelayTime">
          <el-input v-model="form.allowDelayTime" placeholder="请输入每日标准工作时长">
            <template #append>
              在考核时间延后此值后也算为准点
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="站点考核开始时间" prop="assessStartTime">
          <el-time-picker v-model="form.assessStartTime" format="HH:mm" value-format="HH:mm" />
        </el-form-item>
        <el-form-item label="站点考核结束时间" prop="assessEndTime">
          <el-time-picker v-model="form.assessEndTime" format="HH:mm" value-format="HH:mm" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_data_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RouteStationAssess">
import { listRouteStationAssess, getRouteStationAssess, delRouteStationAssess, addRouteStationAssess, updateRouteStationAssess } from "@/api/dispatch/routeStationAssess";

const { proxy } = getCurrentInstance();
const { sys_data_status } = proxy.useDict('sys_data_status');

const props = defineProps({
  // 当前的值
  routeId: [Number, String],
  stationId: [Number, String],
});
const routeStationAssessList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    routeId: undefined,
    stationId: undefined,
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    routeId: [
      { required: true, message: "线路ID不能为空", trigger: "blur" }
    ],
    stationId: [
      { required: true, message: "站点id不能为空", trigger: "blur" }
    ],
    assessTime: [
      { required: true, message: "站点考核时间不能为空", trigger: "blur" }
    ],
    assessStartTime: [
      { required: true, message: "站点考核开始时间不能为空", trigger: "blur" }
    ],
    assessEndTime: [
      { required: true, message: "站点考核结束时间不能为空", trigger: "blur" }
    ],
    allowAheadTime: [
      { required: true, message: "允许提前时间不能为空", trigger: "blur" }
    ],
    allowDelayTime: [
      { required: true, message: "允许延后时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询线路站点考核列表 */
function getList() {
  loading.value = true;
  listRouteStationAssess(queryParams.value).then(response => {
    routeStationAssessList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    routeId: props.routeId,
    stationId: props.stationId,
    assessTime: null,
    assessStartTime: null,
    assessEndTime: null,
    allowAheadTime: null,
    allowDelayTime: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("routeStationAssessRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加线路站点考核";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getRouteStationAssess(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改线路站点考核";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["routeStationAssessRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateRouteStationAssess(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addRouteStationAssess(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除线路站点考核编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delRouteStationAssess(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

watch(props, (newVal, oldVal) => {
  console.log('watch', newVal)
  queryParams.value.routeId = newVal.routeId
  queryParams.value.stationId = newVal.stationId
  getList()
}, { immediate: true });
</script>
